package com.peanut.baby.mvp.profile.moments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.peanut.baby.R;
import com.peanut.baby.model.BBSMoment;
import com.peanut.baby.mvp.BaseMvpFragment;
import com.peanut.baby.mvp.bbs.BBSRecycleListAdapter;
import com.peanut.devlibrary.widget.pullrecycle.PullRecyclerView;
import com.peanut.devlibrary.widget.pullrecycle.layoutmanager.XLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by anonymouslystudio on 2017/8/2.
 */

public class ProfileMomentsFragment extends BaseMvpFragment implements PullRecyclerView.OnRecyclerRefreshListener,
    ProfileMomentsContract.View{
    @BindView(R.id.pull_recycler)
    PullRecyclerView pullRecycler;
    Unbinder unbinder;

    private List<BBSMoment> moments;
    private BBSRecycleListAdapter adapter;

    private String userId = "";
    private int pageNo = 1;
    private int pageSize = 5;
    private ProfileMomentsPresenter presenter;

    public static ProfileMomentsFragment newInstance(String userId){
        ProfileMomentsFragment fg = new ProfileMomentsFragment();
        Bundle bundle = new Bundle();
        bundle.putString("userId",userId);
        fg.setArguments(bundle);
        return fg;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_profile_moments, null);
        unbinder = ButterKnife.bind(this, rootView);
        pullRecycler.enablePullRefresh(false);
        pullRecycler.enableLoadMore(true);
        pullRecycler.setOnRecyclerRefreshListener(this);
        return rootView;
    }

    @Override
    public void initData() {
        userId = getArguments().getString("userId");
        presenter = new ProfileMomentsPresenter(this,getActivity());
        moments = new ArrayList<>();
        adapter = new BBSRecycleListAdapter(getActivity(),moments);
        pullRecycler.setLayoutManager(new XLinearLayoutManager(getActivity()));
        pullRecycler.setAdapter(adapter);

        presenter.getMoments(userId,pageNo,pageSize);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        pageNo++;
        presenter.getMoments(userId,pageNo,pageSize);
    }

    @Override
    public void onGetMomentsSuccess(List<BBSMoment> _moments) {
        if(getActivity() == null){
            return;
        }
        pullRecycler.stopLoadMore();
        if(pageNo == 1){
            moments.clear();
        }
        if(_moments != null && !_moments.isEmpty()){
            moments.addAll(_moments);
        }
        adapter.notifyDataSetChanged();
        pullRecycler.enableLoadMore(_moments != null && _moments.size() >= pageSize);
    }

    @Override
    public void onGetMomentsFailed(String reason) {
        if(getActivity() == null){
            return;
        }
        pullRecycler.stopLoadMore();
        showToast(reason);
        pullRecycler.enableLoadMore(false);
    }

}
